--竞技场头条
---@class BillBoardRankListPanel : UIComponentBase
---@field _parent UISystemBillBoard
local BillBoardRankListPanel = Class("BillBoardRankListPanel",UIComponentBase)
local t = BillBoardRankListPanel

function t:Open(rankType)
    self.bigRankType = rankType
    self.pageIndex = 0
    self.subRankType = 0
    self:ReqSelfRankInfo(0)
end

function t:OnCreate()
    self.ctrlPage = self:GetController("ctrlPage")
    self.rankList = self:GetList("list")
    self.myRank = self:GetChild("playerrank")
    self.myHead = self:GetChild("playerhead/head/HeadIcon")
    self.myHeadFrame = self:GetChild("playerhead/frame")
    --self.myVip = self:GetChild("playervip")
    self.myName = self:GetChild("playername")
    self.myCtrlType = self:GetController("type")
    self.myPower = self:GetChild("playernumber")
    self.myRewardList = self:GetChild("playerrewardlist")
    self.myPartType = self:GetChild("title7")
    self.ctrlOnRank = self:GetController("ctrlOnRank")
    self.btnRight = self:GetChild("buttonRight")
    self.btnLeft = self:GetChild("buttonLeft")
    self.btnReward = self:GetChild("btnReward")
    self.ctrlMyRank = self:GetController("ranktitle")
    self.timeText = self:GetChild("time")

    self.listItemTab = {}
    local listTab = self:GetList("listTab")
    for i = 0,listTab.numChildren-1 do
        local item = {}
        item.obj = listTab:GetChildAt(i)
        item.ctrlLock = item.obj:GetController("lock")
        item.click = item.obj:GetChild("click")
        item.obj:GetChild("title").text = getLanguage("BillboardSubRank"..(5-i))
        --item.ctrlReward = item.obj:GetController("reward")
        item.ctrlLock.selectedIndex = 1
        item.item = item.obj:GetChild("reward")
        table.insert(self.listItemTab,item)
        self:RegisterOnClick(item.obj,self.PageChanged,self,i)
    end
   -- self:RegisterOnChanged(self.ctrlPage,self.PageChanged)
    self.ctrlShowTypeList = self:GetController("subList")
    self.ctrlReward = self:GetController("reward")
    self.poolMgr = ComponentPoolMgr.New(t.RankListItem)
    ---@type BillBoardRankListPanel_Item
    self.firstPlayerItem = self.poolMgr:GetBindScript(self:GetChild("first"))
    self:SetDynamicList(self.rankList,self.ItemRender)
    self:SetDynamicList(self.myRewardList,self.MyRewardItemRender)
    self:RegisterOnClick(self.btnReward,self.ClickReward)
    --self:RegisterOnClick(self.btnLeft,self.ClickLeft)
    --self:RegisterOnClick(self.btnRight,self.ClickRight)
    self.rankDataList = {}
    self.rankList.numItems = 99
    self.ctrlShowTypeList.selectedIndex = 0
    self.pageIndex = 0
    self.nextRewardTime = 0
    self.subRankType = 0
end

function t:OnEnable()
    self.nextRewardTime = self:GetNextRewardTime()
    self:RegisterEvent(Config.EventType.RefreshNoticeSelfRank,self.ResNoticeSelfRankMsg)
    self:RegisterEvent(Config.EventType.RefreshNoticeGetRankReward,self.RefreshNoticeGetRankRewardMsg)
end
function t:OnDisable()
    self:UnRegisterEvent()
    self:RemoveTimeCall(self.RefreshEndTime)
end
function t:OnDestroy()
    if self.poolMgr then
        self.poolMgr:OnDestroy()
    end
    self.poolMgr = nil
    self.firstPlayerItem = nil
end
function t:RefreshEndTime()
    self:RefreshNextRewardTip()
    if logicMgr.NoticeManager.IsTimeEnd() then
        self:RemoveTimeCall(self.RefreshEndTime)
        self:RefreshBtnRank()
        self.ctrlType.selectedIndex = 3
    end
end
function t:GetNextRewardTime()
    local str = dataTable.getGlobalString(118)
    local time = logicMgr.CronMgr.GetNextDateTime(str,HelperFunc.GetServerTime())
    return tonumber(time)
end
function t:RefreshNextRewardTip()
    local time = HelperFunc.GetServerTime()
    if time >= self.nextRewardTime then
        self.nextRewardTime = self:GetNextRewardTime()
        self:ReqSelfRankInfo(self.subRankType)
    end
    self.timeText.text = HelperFunc.GetTimeStr(self.nextRewardTime - time)
end
function t:ReqSelfRankInfo(subRankType)
    NoticeHandler.ReqSelfRankInfo(self.bigRankType,subRankType)
end
function t:Refresh()
    logicMgr.NoticeManager.GetSubRankList(self.bigRankType,self.subRankType,self.rankDataList)
    --first
    local firstData = table.remove(self.rankDataList,1)
    self.firstPlayerItem:Refresh(firstData,true,self.bigRankType,self.subRankType, 1)
    --other
    local exNum = logicMgr.NoticeManager.GetSubRankLimitCount(self.bigRankType,self.subRankType)
    exNum = exNum - #self.rankDataList -1
    self.rankList.numItems = #self.rankDataList + exNum
    self.rankList:ScrollToView(0,false,true)
    self:PageRefresh()
    --my
    self:RefreshMyRank()
    self:RefreshRankRewardBtn()
    --time
    self:RefreshNextRewardTip()
    self:TimeCall(self.RefreshEndTime, 1000, -1)
    self:RefreshEndTime()
end
function t:RefreshMyRank()
    local mgr = logicMgr.NoticeManager
    local myData = mgr.partRank.myRank

    self:SetPlayerIcon(self.myHead,logicMgr.HeroManager.GetRoleInfo("pic"),self.myHeadFrame,logicMgr.HeroManager.GetRoleHeadFrame())--myData.headId)
    self.myName.text = logicMgr.HeroManager.GetRoleName()
    self.myPower.text = getNumString(myData.param)
    self.myCtrlType.selectedIndex = self.bigRankType - 1
    --排名奖励
    local rankLevel = logicMgr.NoticeManager.GetMySelfSubRankLevel()
    if rankLevel > 0 then
        self.ctrlOnRank.selectedIndex = 0
        self.myRewardList.numItems = 0
        self.ctrlMyRank.selectedIndex = rankLevel <= 3 and (rankLevel-1) or 3
        self.myRank.text = string.format(getLanguage("ArenaTips4"),rankLevel)
    else
        self.ctrlOnRank.selectedIndex = 1
    end
    local defaultSubType = logicMgr.NoticeManager.GetDefaultSubRankType(self.bigRankType)
    if defaultSubType > 0 then
        self.myPartType.text = getLanguage("BillboardSubRank"..defaultSubType)
    end
    --上次累计奖励
    local hasReward = mgr.HasRankReward(self.bigRankType)
    if hasReward then
        self.ctrlOnRank.selectedIndex = 0
        self.ctrlReward.selectedIndex = 0
        self.rewards = mgr.GetMyRankRewards(self.bigRankType)
        self.myRewardList.numItems = #self.rewards
    else
        self.ctrlReward.selectedIndex = 1
    end

    self._parent:RefreshCtrlRank()
end
function t:ItemRender(index,go)
    local item = self.poolMgr:GetBindScript(go)
    item:Refresh(self.rankDataList[index],false,self.bigRankType,self.subRankType,index+1)
end
function t:MyRewardItemRender(index,go)
    local data = self.rewards[index]
    CtrlManager.GetCtrl(PanelNames.Item).initData(go,data)
end
--刷新分页
function t:PageRefresh()
    local mgr = logicMgr.NoticeManager
    if not mgr.IsSubRankOpen(self.bigRankType,4) then
        self.ctrlShowTypeList.selectedIndex = 0
        return
    end
    self.ctrlShowTypeList.selectedIndex = 1
    for k,v in pairs(self.listItemTab) do
        local sub = mgr.PartSubType.hero - k + 1
        if sub == mgr.GetDefaultSubRankType(self.bigRankType)  then
            v.ctrlLock.selectedIndex = 2
        else
            v.ctrlLock.selectedIndex = mgr.IsSubRankOpen(self.bigRankType,sub) and 1 or 0
        end
    end
end
--分页
function t:PageChanged(index)
    if self.pageIndex == index then
        return
    end
    local subIndex = 5 - index
    if not logicMgr.NoticeManager.IsSubRankOpen(self.bigRankType,subIndex) then
        --UISysTips.AddMsg(getLanguage("NoticeTips1"))
        self.ctrlPage.selectedIndex = self.pageIndex
        self._parent:ShowLockTip(self.bigRankType,subIndex)
        return
    end
    self.ctrlPage.selectedIndex = index
    self.pageIndex = index
    self.subRankType = subIndex
    NoticeHandler.ReqSelfRankInfo(self.bigRankType,subIndex)
end

function t:RefreshRankRewardBtn()
    local flag = logicMgr.NoticeManager.HasRankReward(self.bigRankType)
    self.btnReward.grayed = not flag
end
function t:ClickReward()
    if not logicMgr.NoticeManager.HasRankReward(self.bigRankType) then
        UISysTips.AddMsg("未到领奖时间")
        return
    end
    NoticeHandler.ReqReceiveSelfAward(self.bigRankType)
end
function t:ResNoticeSelfRankMsg()
    if self.subRankType == 0 then
        self.subRankType = logicMgr.NoticeManager.curSubRankType
        logicMgr.NoticeManager.SetDefaultSubRankType(self.bigRankType,self.subRankType)
    end
    if self.subRankType > 0 then
        self.ctrlPage.selectedIndex = logicMgr.NoticeManager.PartSubType.hero -self.subRankType--self.subRankType -1
        self.pageIndex = self.ctrlPage.selectedIndex
    end
    self:Refresh()
   -- UIRedDots.removeListen("BillBoardRankListPanel"..self.btnReward.name)
   -- UIRedDots.addViewListen(PanelNames.UISystemBillBoard, "BillBoardRankListPanel"..self.btnReward.name, self.btnReward,
   --         Config.RedDotsType.Billboard, self.bigRankType)
end
function t:RefreshNoticeGetRankRewardMsg()
    logicMgr.UIShowManager.ShowRewardByTempItemInfo(logicMgr.NoticeManager.GetMyRankRewards(self.bigRankType))
    self:RefreshMyRank()
    self:RefreshRankRewardBtn()
end



t.RankListItem = Class("RankListItem",UIComponentInterface)
---@class BillBoardRankListPanel_Item : UIComponentInterface
local t_t = t.RankListItem

function t_t:OnCreate()
    self.rankText = self:GetChild("rank")
    self.iconRoot = self:GetChild("icon")
    self.playerIcon = self:GetChild("icon/head/HeadIcon")
    self.playerFrame = self:GetChild("icon/frame")
    self.ctrlEmpty = self:GetController("ctrlEmpty")
    --self.vip = self:GetChild("vip")
    self.ctrlOnline = self:GetController("line")
    self.nameText = self:GetChild("name")
    self.ctrlRankTitle = self:GetController("ranktitle")
    self.ctrlType = self:GetController("type")
    local icon = self:GetChild("icon")
    self.ctrlFirst = icon:GetController("first")
    ---@type GGraph[]
    self.posGraphList = {}
    self.headList = {}
    for i= 0,5 do
        table.insert(self.headList,self:GetChild("head"..i))
     end
    --for i= 1,5 do
    --    table.insert(self.posGraphList,self:GetChild("rolePos"..i))
   -- end
    self.firstRewardList = self:GetList("rewardList")
    self.otherRewardList = self:GetList("rankrewardlist")
    self:SetDynamicList(self.firstRewardList,self.ItemRewardRender,nil,true)
    self:SetDynamicList(self.otherRewardList,self.ItemRewardRender,nil,true)
    self.powerText = self:GetChild("number")
end
function t_t:OnDestroy()
    self.data = nil
    self.rewards = nil
end
function t_t:Empty()
    self.ctrlEmpty.selectedIndex = 1
    self.playerIcon.url = ""
    self.nameText.text = ""
    self.powerText.text = ""
    self.playerFrame.url = ""
    --self.vip.visible = false
    --if self.isFirst then
        --for k,v in pairs(self.headList) do
            --v:GetChild("icon").url = ""
        --end
    --end
end
function t_t:RefreshReward(rewards)
    self.rewards = rewards
    if self.isFirst then
        self.firstRewardList.numItems = #self.rewards
    else
        self.otherRewardList.numItems = #self.rewards
    end
end
function t_t:RefreshFormation()
    if self.isFirst then
        local index = 1
        local ctrl = CtrlManager.GetCtrl(PanelNames.HeadRectItem)
        for k,v in pairs(self.data.formation) do
            local head = self.headList[index]
            if v > 0 then
                if head then
                    head.visible = true
                    local data = TablePool.Get()
                    data.partnerModelId = v
                    data.icon = HeroManager.GetHeroIconByModelId(v)
                    data.quality = HeroManager.GetHeroQualityByModelId(v)
                    ctrl.initData(head,data)
                    TablePool.Release(data)
                    index = index + 1
                end
            end
        end
        for i = index,#self.headList do
            --ctrl.initData(self.headList[i],nil)
            self.headList[i].visible = false
        end
    end
end
---@param data NoticePlayerInfo
function t_t:Refresh(data,isFirst,big,sub,rank)
    self.data = data
    self.isFirst = isFirst
    self.rankLevel = rank
    if rank <= 3 then
        self.ctrlRankTitle.selectedIndex = rank-1
    else
        self.ctrlRankTitle.selectedIndex = 3
        self.rankText.text = string.format(getLanguage("ArenaTips4"),rank )
    end
    if data == nil then
        self:Empty()
        return
    else
        self.ctrlEmpty.selectedIndex = 0
    end
    self.ctrlType.selectedIndex = big - 1
    self.ctrlFirst.selectedIndex = isFirst and 1 or 0
    local rewards = NoticeManager.GetRewards(big,sub,rank)
    self:RefreshReward(rewards)
    --self.vip.visible = true
    self:SetPlayerIcon(self.playerIcon,data.headId,self.playerFrame,data.picFrame)
    --self.vip.text = data.vip
    self.ctrlOnline.selectedIndex = data.online and 0 or 1
    self.nameText.text = data.name
    --[[local tip = ""
    if big == logicMgr.NoticeManager.PartBigType.social then
        tip = getLanguage("BillboardRankSocialTip")
    elseif big == logicMgr.NoticeManager.PartBigType.power then
        tip = getLanguage("BillboardRankPowerTip")
    else
        tip = getLanguage("BillboardRankCostTip")
    end]]
    self.powerText.text = getNumString(data.param)--string.format(tip,data.param)
    self:RefreshFormation()
    self.iconRoot.onClick:Clear()
    self.iconRoot.onClick:Add(self.ClickHead,self)
end
function t_t:ClickHead()
    if self.data.playerId == logicMgr.HeroManager.GetRoleId() then
        return
    end
    roleInfoHandler.sendreqOtherRoleInfo(self.data.playerId);
end
function t_t:ItemRewardRender(index,go)
    local data = self.rewards[index]
    CtrlManager.GetCtrl(PanelNames.Item).initData(go,data)
end
return t